package com.diamond.util;

import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class StringUtil {
	public static String[] toArray(String target, String delim) {
		if (target == null)
			return new String[0];
		StringTokenizer st = new StringTokenizer(target, delim);
		String[] result = new String[st.countTokens()];
		int i = 0;

		while (st.hasMoreTokens()) {
			result[i] = st.nextToken();
			i++;
		}
		return result;
	}
	
	public static String arrayToStr(String[] arr) {
		String str=null;
		if (arr == null)
			return "";
		else{
			for (int i = 0; i < arr.length; i++) {
				str += arr[i] + "\r\n";
			}
		}
		return str;
	}

	public static String replaceStr(String str, String oldStr, String newStr) {
		int s = 0;
		int e = 0;
		int ol = oldStr.length();
		StringBuffer result = new StringBuffer();

		while ((e = str.indexOf(oldStr, s)) >= 0) {
			result.append(str.substring(s, e));
			result.append(newStr);
			s = e + ol;
		}
		result.append(str.substring(s));
		return result.toString();
	}

	public static String ConvertToStr(Object obj) {
		String str = "";
		if (!isEmpty(obj)) {
			str = obj.toString().trim();
		}
		return str;
	}

	public static String ConvertDbStrToStr(Object obj) {
		String str = "";
		if (!isEmpty(obj)) {
			str = obj.toString().trim().toLowerCase();
		}
		if (str.contains("_")) {
			String[] tmp = str.split("_");
			int length = tmp.length;
			str = tmp[0];
			for (int i = 1; i < length; i++) {
				String strtmp = tmp[i];
				str += strtmp.substring(0, 1).toUpperCase() + strtmp.substring(1, strtmp.length());
			}
		}
		return str;
	}

	public static String ConvertToStrs(Object obj) {
		String str = " ";
		if (!isEmpty(obj)) {
			str = obj.toString().trim();
		}
		return str;
	}

	public static boolean isEmpty(Object obj) {
		boolean result = false;
		try {
			if (obj == null)
				return true;
			Class<? extends Object> cls = obj.getClass();

			if (cls.isArray()) {
				Object[] objs = (Object[]) obj;
				if (objs.length <= 0) {
					result = true;
				}
			}
		
			if (obj instanceof java.lang.String && obj.toString().trim().equals("")) {
				result = true;
			}
			if (obj instanceof Collection) {
				@SuppressWarnings(value = { "unchecked" })
				Collection coll = (Collection) obj;
				if (coll.isEmpty()) {
					result = true;
				}
			}
			if (obj instanceof Map) {
				@SuppressWarnings(value = { "unchecked" })
				Map map = (Map) obj;
				if (map.isEmpty()) {
					result = true;
				}
			}
			if (obj instanceof Number) {
				Number num = (Number) obj;
				if (num.toString().trim().equals(""))
					result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String ConvertDecToStr(Object obj, String code) {
		String str = ConvertToStr(obj);
		try {
			if (!str.equals(""))
				str = URLDecoder.decode(str, code);
		} catch (Exception e) {
			// SaveStackTrace(e);
		}
		return str;
	}

	public static String wrapClauses(String[] strs) {
		if (strs == null || strs.length == 0)
			return "('')";
		String rel = "(";
		for (int i = 0; i < strs.length; i++) {
			rel += "'" + strs[i] + "',";
		}
		rel = removeEnd(rel, ",");
		return rel + ")";
	}

	public static String wrapClause(String[] strs) {
		String rel = "";
		for (int i = 0; i < strs.length; i++) {
			rel += "'" + strs[i] + "',";
		}
		rel = removeEnd(rel, ",");
		return rel;
	}

	public static String wrapClauseList(Map<String, List<String>> map) {
		String rel = "";
		if (!isEmpty(map)) {
			for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
				String fieldName = iter.next();
				List<String> valObj = map.get(fieldName);
				rel += " " + fieldName + " in " + wrapMidList(valObj) + " and";
			}
			rel = removeEnd(rel, "and");
		}
		return rel;
	}

	public static String wrapClauseStringArray(Map<String, String[]> map) {
		String rel = "";
		if (!isEmpty(map)) {
			for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
				String fieldName = iter.next();
				String[] valObj = map.get(fieldName);
				rel += " " + fieldName + " in " + wrapClauses(valObj) + " and";
			}
			rel = removeEnd(rel, "and");
		}
		return rel;
	}

	public static String wrapClauseString(Map<String, String> map) {
		String rel = "";
		if (!isEmpty(map)) {
			for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
				String fieldName = (String) iter.next();
				String valObj = map.get(fieldName);
				rel += " " + fieldName + " = " + valObj + " and";
			}
			rel = removeEnd(rel, "and");
		}
		return rel;
	}

	public static String wrapMidList(List<String> detailMidList) {
		if (detailMidList == null || !(detailMidList.size() > 0))
			return "('')";
		String imidStr = "(";
		for (Iterator<String> iter = detailMidList.iterator(); iter.hasNext();) {
			String str = iter.next();
			if (iter.hasNext()) {
				imidStr += "'" + str + "',";
			} else {
				imidStr += "'" + str + "')";
			}

		}
		return imidStr;
	}

	public static List<String> CoverToList(String data) {
		List<String> list = new ArrayList<String>();
		if (data != null && !data.trim().equals("")) {
			String datas[] = data.split(",");
			int num = datas.length;
			for (int i = 0; i < num; i++)
				list.add(datas[i]);
		}
		return list;
	}

	public static String removeEnd(String str, String remove) {
		if (str.equals("") || remove.equals(""))
			return str;
		if (str.endsWith(remove))
			return str.substring(0, str.length() - remove.length());
		else
			return str;
	}

	public static String convertBig5ToUnicode(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("Big5"), "ISO8859_1");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertToUnicode(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes(), "ISO8859_1");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertUnicodeToBig5(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("ISO8859_1"), "Big5");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertUnicodeToGbk(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("utf-8"), "GBK");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertUnicodeToUtf8(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("ISO8859_1"), "utf-8");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}
	public static String convertUtf8ToUnicode(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("utf-8"), "ISO8859_1");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}
	public static String convertLatin1ToUtf8(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("Latin1"), "utf-8");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertToUtf8(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes(), "utf-8");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertBig5ToUtf8(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("Big5"), "utf-8");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String convertUtfToBig5(Object obj) {
		try {
			return new String(ConvertToStr(obj).getBytes("utf-8"), "Big5");
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String splitList(List<String> list) {
		String result = "";
		if (isEmpty(list)) {
			for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
				String str = iter.next();
				result += str + "##";
			}
			result = removeEnd(result, "##");
		}
		return result;
	}
	public static Double doubleFormat(Double input){
		if(input==null)return null;
		DecimalFormat  df  =   new  DecimalFormat("##########.##");
		return Double.parseDouble(df.format(input));
	}
	
	//四舍五入
	public static double doubleFmt(Double input) {
		return Math.round(input);
	}
}
